/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tablasSql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import otros.MiTableModel;

/**
 *
 * @author FERRETERIA
*/

public class Clientes {
        
    public static MiTableModel getClientes(String criterio, String dato) {
        String[] columNames = {"Codigo","NIF / DNI", "Nombre Comercial", "Nombre Fiscal", "Telefono"}; 
        String[] tableNames = {"Codigo","nif","nombrecomercial", "nombrefiscal",  "telefono1"};
        Class [] tipos = {Integer.class, String.class, String.class, String.class, String.class}; 
                    
        String sql = "SELECT * from nclientes";
        String contar = "nclientes";
        String aux = "";
        if (!dato.isEmpty()) {
            if (criterio.compareTo("codigo") == 0) 
                aux += " where " + criterio + " like '" + dato + "'";
            else
                aux += " where " + criterio + " like '%" + dato + "%'";     
        }
        sql += aux;
        contar += aux;
         
        Object[][] dtDatos = new Sql().GetTabla(tableNames, contar, sql, tipos);
        if (dtDatos.length > 0) {
            return new MiTableModel(dtDatos,columNames);        
        } 
        else
            return null;
    }

    
    public static ResultSet getCliente (String cod) {
        String sql = "Select * from nclientes where codigo = '" + cod + "'";
        return new Sql().getDatos(sql);
    }
    
    public static String getNomCliente (String cod) {
        try {
            String sql = "Select * from nclientes where codigo = '" + cod + "'";
            ResultSet res = new Sql().getDatos(sql);
            res.next();
            return res.getString("NombreComercial");
        } catch (SQLException ex) {
            return "Error en codigo de cliente";
        }
    }
    
    public static float getCredito(String cod) {
        try {
            ResultSet res = getCliente(cod);
            if (res.next())
                return res.getFloat("credito");
        } catch (SQLException ex) {
        }
        return 0;
    }
    
    public static void guardar (String [] datos, boolean guardar) {
        String sql = "";   
        int numString = 10;
        int numFloat = 14;
        if (!guardar) {
           sql = "update nclientes set nombrecomercial = ?, nombrefiscal = ?, domicilio = ?,";
           sql += "localidad = ?, codigopostal = ?, nif = ?, telefono1 = ?, telefono2 = ?,";
           sql += "email = ?, fax = ?, descPie = ?, descLinea = ?, margen=?,credito=?, copiasalbaranes = ?, copiasfacturas = ?,"; 
           sql += " tipopago = ? where codigo = ?";
           //numString = 12;
           
        }
        else {
            
            sql = "insert into nclientes (nombrecomercial,nombrefiscal,domicilio," +
                        "localidad, codigopostal,nif,telefono1,telefono2,email,fax,creado,descPie,descLinea,margen," +
                    "credito,copiasalbaranes,copiasfacturas, tipopago, codigo) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            numString = 11;
            numFloat = 15;
        }
        new Sql().actualizarDatos(sql, datos, numString, numFloat);
    }
    
    public static void borrar(String id) {
        String sql = "delete from nclientes where codigo = " + id;
        new Sql().hacerConsulta(sql);
    }
    
    public static int getCantFac(int id, int tipo) {
        String tabla = FacturasAlbaranes.getTablaSql(tipo);
        String sql = "select count(*) as cantidad from " + tabla + " where idcliente = " + id;
        ResultSet re = new Sql().getDatos(sql);
        try {
            if (re.next())
                return re.getInt("cantidad");
        } catch (SQLException ex) {
            System.out.println("Clientes.getCantFac: " + ex);
            return 0;
        }
        return 0;
    }
    
    public static void guardarUltTicket(String[]datos, int tipo) {
        String campo = "";
        switch(tipo) {
            case 0:
                campo = "idultFac";
                break;
            case 1:
                campo = "idultalb";
                break;
            default:
                return;
        }
        String sql = "update nclientes set " + campo + " = ? where codigo = ?";
        new Sql().actualizarDatos(sql, datos, 1, 0);
    }
    
    public static String buscarClienteEnFacturas(String id) {
        String tablas = "";
        for (int i = 0; i < FacturasAlbaranes.NUMTABLAS; i++) {
            String tabla = FacturasAlbaranes.getTablaSql(i);
            String sql = "select * from "+tabla+" where idcliente="+id;
            ResultSet re = new Sql().getDatos(sql);
            try {
                if (re.next()) {
                    if ((i>0)&& !tablas.isEmpty()) tablas+=", ";
                    tablas+= FacturasAlbaranes.getTablaSql(i);
                }
            } catch (SQLException ex) {
                System.out.println("Clientes.getCantFac: " + ex);
            }
        }
        return tablas;
    }
}
